package com.pengpai.pangucms.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pengpai.pangucms.entity.SysRolePermission;
import com.pengpai.pangucms.mapper.SysRolePermissionMapper;
import com.pengpai.pangucms.service.ISysRolePermissionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.yaml.snakeyaml.events.Event;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * <p>
 * 角色和菜单关联表 服务实现类
 * </p>
 *
 * @author lijunjie
 * @since 2023-07-30
 */
@Service
public class SysRolePermissionServiceImpl extends ServiceImpl<SysRolePermissionMapper, SysRolePermission> implements ISysRolePermissionService {


    @Resource
    SysRolePermissionMapper sysRolePermissionMapper;

    @Override
    public void updateRolePermission(Long roleId, String ids) {
        List<Long> list = null;
        if(StrUtil.isNotBlank(ids)){
            list = Stream.of(ids.split(",")).map(Long::parseLong).collect(Collectors.toList());
        }
        //现根据roleid删除现有的权限
        QueryWrapper<SysRolePermission> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("role_id", roleId);
        sysRolePermissionMapper.delete(queryWrapper);
        //根据页面传的参数进行数据更新
        List<SysRolePermission> sysRolePermissions = new ArrayList<>();
        for (Long id :list){
            SysRolePermission sysRolePermission = new SysRolePermission();
            sysRolePermission.setRoleId(roleId);
            sysRolePermission.setPermissionId(id);
            sysRolePermissions.add(sysRolePermission);
        }
        this.saveBatch(sysRolePermissions);


    }
}
